package config

import "github.com/spf13/viper"

type Database struct {
	Dbtype     string
	Host       string
	Port       int
	Name       string
	Username   string
	Password   string
	Sqlitefile string
}

func InitDatabase(cfg *viper.Viper) *Database {
	dbType := cfg.GetString("dbType")
	if dbType == "mysql" {
		mc := cfg.Sub("mysql")
		return &Database{
			Dbtype:   dbType,
			Port:     mc.GetInt("port"),
			Host:     mc.GetString("host"),
			Name:     mc.GetString("name"),
			Username: mc.GetString("username"),
			Password: mc.GetString("password"),
		}
	} else if dbType == "sqlite" {
		sc := cfg.Sub("sqlite")
		return &Database{
			Dbtype:     dbType,
			Sqlitefile: sc.GetString("sqlitefile"),
		}
	}

	return nil
}

var DatabaseConfig = new(Database)
